package com.tanhua.admin.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.api.service.MovementApi;
import com.tanhua.api.service.UserInfoApi;
import com.tanhua.api.service.VideoApi;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.mongo.Video;
import com.tanhua.model.pojo.UserInfo;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.VideoVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class ManageService {

    @DubboReference
    private UserInfoApi userInfoApi;

    @DubboReference
    private VideoApi videoApi;

    @DubboReference
    private MovementApi movementApi;

    // 分页查询用户列表
    public PageResult<UserInfo> findByPage(Integer page, Integer pagesize, Long id, String nickname, String city) {

        Page<UserInfo> pageInfo = userInfoApi.findByPage(page, pagesize, id, nickname, city);

        return new PageResult<>(page, pagesize, pageInfo.getTotal(), pageInfo.getRecords());
    }

    // 查询用户的详情数据
    public UserInfo findByUserId(Long userId) {
        return userInfoApi.findById(userId);
    }

    // 查询用户的视频数据
    public PageResult<VideoVo> findByPageVideo(Integer page, Integer pagesize, Long uid) {

        PageResult<Video> pageResult = videoApi.findPageVideo(uid, page, pagesize);

        List<Video> videoList = pageResult.getItems();

        if (CollUtil.isEmpty(videoList)) {
            return null;
        }

        // 抽取用户id
        List<Long> userIds = videoList.stream().map(Video::getUserId).collect(Collectors.toList());

        // 获取用户详情数据
        Map<Long, UserInfo> map = userInfoApi.findByIds(userIds).stream().collect(Collectors.toMap(UserInfo::getId, Function.identity()));

        // 封装数据
        List<VideoVo> list = new ArrayList<>();
        for (Video video : videoList) {
            UserInfo userInfo = map.get(video.getUserId());
            if (userInfo != null) {
                VideoVo vo = VideoVo.init(userInfo, video);

                list.add(vo);
            }
        }

        PageResult<VideoVo> result = new PageResult<>();
        BeanUtils.copyProperties(pageResult, result);
        result.setItems(list);
        return result;
    }

    // 用户动态数据
    public PageResult<MovementsVo> findByPageMovement(Integer page, Integer pagesize, Long uid, Integer state) {

        PageResult<Movement> pageResult = movementApi.findByPageMovement(page, pagesize, uid, state);

        List<Movement> movementList = pageResult.getItems();

        if (CollUtil.isEmpty(movementList)) {
            return null;
        }

        List<Long> userIds = movementList.stream().map(Movement::getUserId).collect(Collectors.toList());

        Map<Long, UserInfo> map = userInfoApi.findByIds(userIds).stream().collect(Collectors.toMap(UserInfo::getId, Function.identity()));

        // 封装数据
        List<MovementsVo> list = new ArrayList<>();
        for (Movement movement : movementList) {
            UserInfo userInfo = map.get(movement.getUserId());
            if (userInfo != null) {
                MovementsVo vo = MovementsVo.init(userInfo, movement);

                list.add(vo);
            }
        }

        PageResult<MovementsVo> result = new PageResult<>();
        BeanUtils.copyProperties(pageResult, result);
        result.setItems(list);

        return result;

    }
}
